/**
* @file         
* @brief		struct 对齐与位域测试.
* @details	    基本功能演示.
                参考文献：https://zhuanlan.zhihu.com/p/354232756
* @author		conkty@126.com
* @date		    2023
* @version	    v0.1
* @par 更新记录:         
*	v0.1: 初建\n
*/
#include <stdio.h>


struct test1
{
    char a;   // 1
    char b;   // 1
    char c;   // 1
} t1;

struct test2
{
    char a;    // 1
    short b;  // 2
    char c;    // 1
} t2;

struct test3
{
    char a;    // 1
    int  b;      // 4
    short c;   // 2
} t3;

struct _PRCODE
{
    unsigned int code1: 2;
    unsigned int cdde2: 2;
    unsigned int code3: 8;
} pc;


struct box 
{
    unsigned int a  : 1;
    unsigned int     : 3;
    unsigned int b  : 4;
} b;

struct stuff1 
{
 unsigned int field1: 30;
 unsigned int field2: 4;
 unsigned int field3: 3;
} s1;

struct stuff2
{
 unsigned int field1: 30;
 unsigned int       : 2;
 unsigned int field2: 4;
 unsigned int       : 0;
 unsigned int field3: 3; 
} s2;

int main()
{
    // sizeof给出的占字节数.
    printf("char size: %lu \n", sizeof(char));
    printf("short size: %lu \n", sizeof(short));
    printf("int size: %lu \n", sizeof(int));
    printf("unsigned int size: %lu \n", sizeof(unsigned int));

    // 经测试，gcc编译器无法对struct类型使用sizeof，故使用变量.
    printf("test1 size: %lu \n", sizeof(t1));
    printf("test2 size: %lu \n", sizeof(t2));
    printf("test3 size: %lu \n", sizeof(t3));
    printf("_PRCODE size: %lu \n", sizeof(pc));
    printf("box size: %lu \n", sizeof(b));
    printf("stuff1 size: %lu \n", sizeof(s1));

    // 报 error: invalid application of 'sizeof' to bit-field
    // 故无法用sizeof获得 bit-field 成员大小
    // printf("stuff1.field1 size: %lu \n", sizeof(s1.field1));
    printf("stuff2 size: %lu \n", sizeof(s2));

    return 0;
}